//获取应用实例
const app = getApp()
import api from '../../utils/api'

Page({
  data: {
    cartList: [],
    zcList: [], 
    wcList: [], 
    ycList: [],
    empty: true,
    total: 0,
    switchFlag:false,
    zcChecked:false,
    wcChecked: false,
    ycChecked: false,
  },
  onLoad: function (options) {
    
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    this.updCartList();
    this.setData({
      zcChecked: false,
      wcChecked: false,
      ycChecked: false,
    })
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },
  onPullDownRefresh: function () {
    // Do something when pull down.
  },
  onReachBottom: function () {
    // Do something when page reach bottom.
  },
  onShareAppMessage: function () {
    // return custom share data when user share.
  },
  onPageScroll: function () {
    // Do something when page scroll
  },
  onTabItemTap(item) {
    
  },
  category: function (list) {
    let zcList = [];
    let wcList = [];
    let ycList = [];
    for (let i = 0; i < list.length; i++) {
      if (list[i].meals == "早餐") {
        zcList.push(list[i]);
      }
      if (list[i].meals == "午餐") {
        wcList.push(list[i]);
      }
      if (list[i].meals == "晚餐") {
        ycList.push(list[i]);
      }
    }
    this.setData({
      zcList: zcList, wcList: wcList, ycList: ycList
    })
  },
  updCartList() { // 更新购物列表
    let user = wx.getStorageSync("user");
    const _this = this;
    let param = { "patientId": user.bh }
    api.POST({
      url: 'getOrder.action',
      params: param,
      success: function (res) {
        if (res.data.success == "true") {
          let flag = res.data.data.length > 0 ? false : true;
          _this.category(res.data.data);
          _this.setData({
            cartList: res.data.data,
            empty: flag
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: '获取订单失败',
          })
        }

      },
      fail: function () {
        wx.showToast({
          icon: 'none',
          title: '网络错误',
        })
      },
    })
  },
  switchChange(e){
    this.setData({
      switchFlag: e.detail.value
    })
  },
  checkboxChange:function(e){
    let that = this;
    let check = false;
    let cb = e.currentTarget.dataset.cb;
    let index = e.currentTarget.dataset.listindex;
    if (e.detail.value == '') {
      check = false
    } else {
      check = true;
    }
    if(cb == 1){
      let zcChecked=true;
      let zcList = that.data.zcList;
      zcList[index].checked = check;
      for(let i=0;i<zcList.length;i++){
        if (!zcList[i].checked){
          zcChecked=false;
          break;
        }
      }
      that.setData({ zcList: zcList, zcChecked: zcChecked })
    }
    if (cb == 2) {
      let wcChecked = true;
      let wcList = that.data.wcList;
      wcList[index].checked = check;
      for (let i = 0; i < wcList.length; i++) {
        if (!wcList[i].checked) {
          wcChecked = false;
          break;
        }
      }
      that.setData({ wcList: wcList, wcChecked: wcChecked })
    }
    if (cb == 3) {
      let ycChecked = true;
      let ycList = that.data.ycList;
      ycList[index].checked = check;
      for (let i = 0; i < ycList.length; i++) {
        if (!ycList[i].checked) {
          ycChecked = false;
          break;
        }
      }
      that.setData({ ycList: ycList, ycChecked: ycChecked })
    }
  },
  // 全选
  selectAll: function (e) {
    let that = this;
    let check=false;
    if (e.detail.value == '') {
      check = false
    }else {
      check = true;
    }
    let cb = e.currentTarget.dataset.cb;
    if(cb==1){
      let zcList = that.data.zcList;
      zcList.filter(item =>{
        item.checked = check;
      })
      that.setData({zcList:zcList})
    }
    if (cb == 2) {
      let wcList = that.data.wcList;
      wcList.filter(item => {
        item.checked = check;
      })
      that.setData({ wcList: wcList })
    }
    if (cb == 3) {
      let ycList = that.data.ycList;
      ycList.filter(item => {
        item.checked = check;
      })
      that.setData({ ycList: ycList })
    }
    
  },
  openRetire:function(){
    let goodList = [];
    let {zcList,wcList,ycList } = this.data;
    zcList.filter(item => {
      if (item.checked){
        goodList.push(item);
      }
    })
    wcList.filter(item => {
      if (item.checked) {
        goodList.push(item);
      }
    })
    ycList.filter(item => {
      if (item.checked) {
        goodList.push(item);
      }
    })
    if(goodList.length<=0){
      wx.showToast({
        icon: 'none',
        title: '请选择数据',
      })
      return;
    }
    console.log(goodList)
    app.globalData.retireList=goodList;
    wx.navigateTo({
      url: '../retire/retire'
    })
  }
})